﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using System.Data.SqlClient;

namespace DataLayer
{
    public static class DatRol
    {
        public static dsBoleto getRoles(string description)
        {
            Util.DBParameter paramRol;
            paramRol.dbType = System.Data.DbType.String;
            paramRol.parameterName = "@Descripcion";
            paramRol.value = description;

            SqlCommand este = Util.SQLCommandBuild("getRoles", paramRol);
            List<string> Tablas = new List<string>();
            
            Tablas.Add("Rol");

            return Util.SelectSPtoDatasetSolve(este, Tablas.ToArray());
        }

        public static bool UpdateRol(List<Util.DBParameter> MisParams)
        {
            try
            {
                Util.SQLCommandBuild("RolUpdate", MisParams).ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("error{0}",ex.Message);
                return false;
            }
        }

        public static int newRol(string nombre)
        {
            Util.DBParameter rol;
            List<Common.Util.DBParameter> parameters = new List<Util.DBParameter>();

            rol.dbType = System.Data.DbType.String;
            rol.parameterName = "@DescRol";
            rol.value = nombre;
            parameters.Add(rol);

            SqlCommand miCommand = Util.SQLCommandBuild("RolIns", parameters);

            return int.Parse(miCommand.ExecuteScalar().ToString());
        }

        public static bool newRolFuncionalidades(int idNewRol, List<int> funcionalidades)
        {
            StringBuilder sbSelect = new StringBuilder();
            sbSelect.Append("(");
            for (int i = 0; i < funcionalidades.Count; i++)
            {
                sbSelect.Append("(SELECT ");
                sbSelect.Append(idNewRol);
                sbSelect.Append(" IdRol, ");
                sbSelect.Append(funcionalidades[i]);
                sbSelect.Append(" IdFuncionalidad) ");
                if (i != funcionalidades.Count - 1)
                {
                    sbSelect.Append("UNION ");
                }
            }
            sbSelect.Append(")");
            Util.DBParameter pQuery;
            pQuery.dbType = System.Data.DbType.String;
            pQuery.parameterName = "@QueryUnions";
            pQuery.value = sbSelect.ToString();

            try
            {
                Util.SQLCommandBuild("RolFuncionalidadesIns", pQuery).ExecuteNonQuery();
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("error{0}", ex.Message);
                return false;
            }
        }
    }
}
